<?php
/**
 * 自定义商品管理 2016-4-12
 */
namespace AdminV3\Controller;
use Think\Page;


class GoodsController extends CommonController{

    public function index()
    {
        $map = array();
        $map['name'] = trim(I('get.name'));
        $map['title'] = trim(I('get.title'));
        $map['number'] = trim(I('get.number'));
        $map['st'] = trim(I('get.st'));


        // 用于区分数据状态 1 启用 2 下架 3 删除
        empty($map['st'])?$map['st']=array('in','1,2'):$map['st'];

        $search=$map;

        $p= empty($_GET['p']) ? 1 : $_GET['p'];
        $Model=D('Goods');
        $list=$Model->findData($map, $p, session('page'));

        if(!empty($list)){
            $voListId =implode(',',array_unique(array_column($list,'movie_v3_id'))) ;
            $movieList =M('Movie_v3')->where('id in('.$voListId.')')->field('id,name')->select();
            //栏目id
            $voListColumnsId=implode(',',array_unique(array_column($list,'movie_level2_v3_id'))) ;
            $columnsList = M('MovieLevel2_v3')->where('id in('.$voListColumnsId.')')->field('id,name')->select();

            foreach($list as $voList_key => $voList_val){
                foreach($movieList as $movieList_key => $movieList_val)
                {
                    if($voList_val['movie_v3_id'] == $movieList_val['id']){
                        $list[$voList_key]['movie_v3_name']=$movieList_val['name'];
                    }
                }

                foreach($columnsList as $columnsList_key => $columnsList_val ){
                    if($voList_val['movie_level2_v3_id'] == $columnsList_val['id']){
                        $list[$voList_key]['movie_level2_v3_name']=$columnsList_val['name'];
                    }
                }
            }
        }

        //dump($list);die();

        foreach($list as $l_key=>$l_val)
        {
            $list[$l_key]['size']=json_decode($l_val['size'],true);
            $list[$l_key]['picture']=json_decode($l_val['picture'],true);
            $list[$l_key]['details_picture']=json_decode($l_val['details_picture'],true);
            $list[$l_key]['base_info']=json_decode($l_val['base_info'],true);
        }

        $count = $Model->getDataNum($map);
        $Page = new Page($count,session('page'));
        foreach ($map as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->assign('search',$search);


        //用于跨页面传递 参数。
        session('goods_url_parameters',base64_encode(serialize($_GET)));

        $this->display();
    }

    public function add()
    {
        if (!empty($_POST)) {

            //dump($_POST);die();

            $data['brand'] = trim(I('post.brand'));
            $data['name'] = trim(I('post.name'));
            $data['title'] = trim(I('post.title'));
            $data['size'] = $_POST['size'];
            $data['color'] = trim(I('post.color'));
            $data['movie_v3_id'] = trim(I('post.movie_v3_id'));
            $data['movie_level2_v3_id'] = trim(I('post.movie_level2_v3_id'));
            $data['price'] = trim(I('post.price'));
            $data['sale_price'] = trim(I('post.sale_price'));
            $data['number'] = trim(I('post.number'));
            $data['status'] = trim(I('post.status'));
            $data['details_info'] = trim($_POST['details_info']);
            $data['relevance_goods_id'] = trim($_POST['relevance_goods_id']);

            $size=json_encode($data['size']);
            if($size){
                $data['size']=$size;
            }

            //基础参数
            $base=array();
            foreach($_POST['base_key'] as $key=>$val){
                $base_temp=array();
              //trim 去除空字符
                if(trim($val)){
                    if(trim($_POST['base_val'][$key])){
                       $base_temp['name']=$val;
                        $base_temp['val']=$_POST['base_val'][$key];
                        $base[]=$base_temp;
                    }
                }
            }

            if($base){
                $base=json_encode($base);
                if($base){
                    $data['base_info']=$base;
                }
            }else{
                $this->error('基础参数没有!');
            }


            $data['details_picture']='';

            $str= preg_match_all('/http:\/\/[\S]+\w/',$data['details_info'],$match);
            $details_price=json_encode($match[0]);
            if($str && $details_price){
                $data['details_picture']=$details_price;
            }

            //商品图片
            if(!empty ($_FILES)){
                foreach ($_FILES as $name => $value) {
                    if (is_file($value['tmp_name'])) {
                        $this->_uploadImage($data, $name, $value);
                    }
                }
            }

            $data['picture']=array();
            for($i=1;$i<6;$i++){
                if($data["picture{$i}"]){
                    $data['picture'][$i]=$data["picture{$i}"];
                    unset($data["picture{$i}"]);
                }

            }

            $picture=json_encode($data['picture']);
            if($picture){
                $data['picture']=$picture;
            }else{
                $data['picture']='';
            }

            if(in_array('',$data)){
                $this->error('数据不完整');
            }

            $data['create_time'] = time();
            $data['last_update_time'] = time();


               // dump($data);die();
            if (M('Goods')->add($data)) {
                $this->redirect('Goods/index');
            } else {
                $this->error('添加失败');
            }
        } else {

            $movieList = D('Movie')->getMoviesIdName();
            $this->assign('movieList',$movieList);

            $this->display();
        }
    }

    public function edit()
    {
        if (!empty($_GET)) {
            $id = I('id');
            if (!empty($id)) {
                $data=D('Goods')->getOneData($id);

                $data['size']=json_decode($data['size'],true);
                $data['picture']=json_decode($data['picture'],true);
                $data['base_info']=json_decode($data['base_info'],true);

                $this->assign('data', $data);

                //$movieList = D('Movie')->getMoviesIdName();
				$movieList = array();
                $this->assign('movieList',$movieList);

                $where=array();
                $where['movie_id']=$data['movie_id'];
                $where['status']=1;
                $movie_level2 =M('MovieLevel2')->where($where)->field('id,name,sort')->select();

                if($movie_level2) {
                    $sortArr = array();
                    foreach ($movie_level2 as $key => $val) {
                        $sortArr[] = $val['sort'];
                    }
                    array_multisort($sortArr, SORT_ASC, SORT_NUMERIC, $movie_level2);
                    $this->assign('movie_level2',$movie_level2);
                }

                $this->display();
            } else {
                $this->error('非法操作! 没有ID值.');
            }
        }


        if (!empty($_POST)) {
            $data['id']=trim(I('post.id'));
            $data['brand'] = trim(I('post.brand'));
            $data['name'] = trim(I('post.name'));
            $data['title'] = trim(I('post.title'));
            $data['size'] = $_POST['size'];
            $data['color'] = trim(I('post.color'));
            $data['movie_v3_id'] = trim(I('post.movie__v3id'));
            $data['movie_level2_v3_id'] = trim(I('post.movie_level2_v3_id'));
            $data['price'] = trim(I('post.price'));
            $data['sale_price'] = trim(I('post.sale_price'));
            $data['number'] = trim(I('post.number'));
            $data['status'] = trim(I('post.status'));
            $data['details_info'] = trim($_POST['details_info']);
            $data['relevance_goods_id'] = trim($_POST['relevance_goods_id']);

            $size=json_encode($data['size']);
            if($size){
                $data['size']=$size;
            }

            //基础参数
            $base=array();
            foreach($_POST['base_key'] as $key=>$val){
                $base_temp=array();
                //trim 去除空字符
                if(trim($val)){
                    if(trim($_POST['base_val'][$key])){
                        $base_temp['name']=$val;
                        $base_temp['val']=$_POST['base_val'][$key];
                        $base[]=$base_temp;
                    }
                }
            }

            if($base){
                $base=json_encode($base);
                if($base){
                    $data['base_info']=$base;
                }
            }else{
                $this->error('基础参数没有!');
            }


            $data['details_picture']='';

            $str= preg_match_all('/http:\/\/[\S]+\w/',$data['details_info'],$match);
            $details_price=json_encode($match[0]);
            if($str && $details_price){
                $data['details_picture']=$details_price;
            }

            //商品图片
            $temp=array();
            if(!empty ($_FILES)){
                foreach ($_FILES as $name => $value) {
                    if (is_file($value['tmp_name'])) {
                        $this->_uploadImage($temp, $name, $value);
                    }
                }
            }

            for($i=1;$i<6;$i++){
                if($temp["picture{$i}"]){
                    $temp[$i]=$temp["picture{$i}"];
                    unset($temp["picture{$i}"]);
                }else{
                    if($temp['stage_photo']){
                        $data['stage_photo']=$temp['stage_photo'];
                    }
                    if($temp['square_photo']){
                        $data['square_photo']=$temp['square_photo'];
                    }
                    if($temp['roundness_photo']){
                        $data['roundness_photo']=$temp['roundness_photo'];
                    }
                }
            }

            //新就图片数据对比
            $old_picture=M('Goods')->where('id='.$data['id'])->field('picture')->find();
            $old_picture=json_decode($old_picture['picture'],true);

            $new_picture=array();
            for($i=1;$i<6;$i++){
                if($temp[$i]){
                    $new_picture[$i]=$temp[$i];
                }elseif($old_picture[$i]){
                    $new_picture[$i]=$old_picture[$i];
                }
            }

            $data['picture']=$new_picture;

            $picture=json_encode($data['picture']);
            if($picture){
                $data['picture']=$picture;
            }else{
                $data['picture']='';
            }

            if(in_array('',$data)){
                $this->error('数据不完整');
            }

            $data['last_update_time'] = time();

                if (M('Goods')->save($data) > 0) {
                    $parameters = unserialize(base64_decode(session('goods_url_parameters')));
                    $this->redirect('Goods/index',$parameters);

                } else {
                    $this->error('编辑信息失败!');
                }
            }

    }

    public function alter()
    {
        $idList = I('post.idlist');
        $type = I('post.type');
        $result = array();
        if (!empty($type)) {
            if (!empty($idList)) {
                $idList = rtrim($idList, ',');
                //var_dump($idList);
                $idList = explode(',', $idList);
                //var_dump($idList);
                $data['ok'] = 0;
                $data['error'] = 0;

                foreach ($idList as $k => $val) {
                    //通过审核
                    switch ($type) {
                        case 'on':
                            $newType = 1;
                            break;
                        case 'off':
                            $newType = 2;
                            break;
                        case 'del':
                            $newType = 3;
                            break;
                    }

                    $re = M('Goods')->where('id=' . $val)->setField('status', $newType);
                    if ($re > 0) {
                        $data['ok']++;
                    } else {
                        $data['error']++;
                    }
                }
                $result['mes'] = "操作成功:{$data['ok']},失败{$data['error']}";
                $this->ajaxReturn($result);

            } else {
                $result['mes'] = '没有选择的值';
                $this->ajaxReturn($result);
            }
        } else {
            $result['mes'] = '非法操作!';
            $this->ajaxReturn($result);
        }

    }

    public function delList()
    {





        $map = array();
        $map['name'] = trim(I('get.name'));
        $map['title'] = trim(I('get.title'));
        $map['number'] = trim(I('get.number'));

        // 用于区分数据状态 1 启用 2 下架 3 删除
        $map['st']=3;


        $search=$map;

        $p= empty($_GET['p']) ? 1 : $_GET['p'];
        $Model=D('Goods');
        $list=$Model->findData($map, $p, session('page'));

        if(!empty($list)){
            $voListId =implode(',',array_unique(array_column($list,'movie_v3_id'))) ;
            $movieList =M('Movie_v3')->where('id in('.$voListId.')')->field('id,name')->select();
            //栏目id
            $voListColumnsId=implode(',',array_unique(array_column($list,'movie_level2_v3_id'))) ;
            $columnsList = M('MovieLevel2_v3')->where('id in('.$voListColumnsId.')')->field('id,name')->select();

            foreach($list as $voList_key => $voList_val){
                foreach($movieList as $movieList_key => $movieList_val)
                {
                    if($voList_val['movie_v3_id'] == $movieList_val['id']){
                        $list[$voList_key]['movie_v3_name']=$movieList_val['name'];
                    }
                }

                foreach($columnsList as $columnsList_key => $columnsList_val ){
                    if($voList_val['movie_level2_v3_id'] == $columnsList_val['id']){
                        $list[$voList_key]['movie_level2_v3_name']=$columnsList_val['name'];
                    }
                }
            }
        }

        //dump($list);die();

        foreach($list as $l_key=>$l_val)
        {
            $list[$l_key]['size']=json_decode($l_val['size'],true);
            $list[$l_key]['picture']=json_decode($l_val['picture'],true);
            $list[$l_key]['details_picture']=json_decode($l_val['details_picture'],true);
            $list[$l_key]['base_info']=json_decode($l_val['base_info'],true);
        }

        $count = $Model->getDataNum($map);
        $Page = new Page($count,session('page'));
        foreach ($map as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->assign('search',$search);

        //用于跨页面传递 参数。
        session('goods_url_parameters',base64_encode(serialize($_GET)));

        $this->display();
    }

}